/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package aptech.semester.code.common;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Calendar;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JTable;
import javax.swing.table.TableModel;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.imgscalr.Scalr;

/**
 *
 * @author vi
 */
public class ApplicationCommon {
    
    public static Rectangle getRectangleForCenter(int mainW, int mainH, int dialogW, int dialogH) {
        return new Rectangle((mainW - dialogW) / 2 , (mainH - dialogH) / 2, dialogW, dialogH);
    }
    
    public static File getAvatarFormDB(byte[] bytes) {
        File file = null;
        try {
            file = File.createTempFile(Calendar.getInstance().getTimeInMillis()+"", ".png");
            
            OutputStream targetFile = new FileOutputStream(file);
            targetFile.write(bytes);
            targetFile.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return file;
    }
    
    public ImageIcon getIconForAvatar(File file) {
        ImageIcon icon = null;
        if (file != null) {
            BufferedImage avatar;
            try {
                avatar = ImageIO.read(file);
                icon = new ImageIcon(avatar);
            } catch (IOException ie) {
                ie.printStackTrace();
            }
        } else {
            icon = new ImageIcon(getClass().getResource("/images/icon/avatar.png"));
        }
        return icon;    
    }
    
    public void writeToExcell(JTable table, String path, String title) {

        try {
            Workbook wb = new XSSFWorkbook(); //Excell workbook
            Sheet sheet = wb.createSheet(); //WorkSheet
            TableModel model = table.getModel(); //Table model

            // title
            Row rowTitle = sheet.createRow(0);
            CellStyle titleStyle = wb.createCellStyle();
            Font font = wb.createFont();
            font.setColor(HSSFColor.BLACK.index);
            font.setBoldweight(Font.BOLDWEIGHT_BOLD);
            titleStyle.setFont(font);
            Cell cellTitle = rowTitle.createCell(7);
            cellTitle.setCellStyle(titleStyle);
            cellTitle.setCellValue(title);

            //header
            Row headerRow = sheet.createRow(2); //Create row at line 0
            CellStyle headerStyle = wb.createCellStyle();
            Font fontHeader = wb.createFont();
            font.setColor(HSSFColor.BLACK.index);
            font.setBoldweight(Font.BOLDWEIGHT_BOLD);
            headerStyle.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
            headerStyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
            headerStyle.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
            headerStyle.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
            headerStyle.setFont(fontHeader);
            for (int headings = 0; headings < model.getColumnCount(); headings++) { //For each column
                Cell cellHeader = headerRow.createCell(headings);
                cellHeader.setCellStyle(headerStyle);
                cellHeader.setCellValue(model.getColumnName(headings));//Write column name
            }

            // data
            Row dataRow = sheet.createRow(3);
            CellStyle dataStyle = wb.createCellStyle();
            dataStyle.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
            dataStyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
            dataStyle.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
            dataStyle.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
            for (int rows = 0; rows < model.getRowCount(); rows++) { //For each table row
                for (int cols = 0; cols < table.getColumnCount(); cols++) { //For each table column
                    Cell cellData = dataRow.createCell(cols);
                    cellData.setCellStyle(dataStyle);
                    cellData.setCellValue(model.getValueAt(rows, cols).toString()); //Write value
                }

                //Set the row to the next one in the sequence 
                dataRow = sheet.createRow((rows + 4));
            }
            wb.write(new FileOutputStream(path.toString()));//Save the file  
        } catch (Exception e) {
        }

    }
}
